<?php
include './MinPHP/run/init.php';
defined('API') or exit();
$sql = "select a.*,b.devUrl,b.productUrl from api a,cate b where a.aid=b.aid and a.id=" . $_GET ['id'];
$row = find ( $sql );
$parameter = unserialize ( $row ['parameter'] );

if(is_supper()){
	$uid = session('id');
	$sql = "select val from cate_setting where aid={$row['aid']} and type=0 and uid={$uid}";
	$cateSetting = find ( $sql );
	$localUrl = $cateSetting['val'];
	if(empty($localUrl)){
		$localUrl="";
	}
}
function getRealUrl($baseUrl, $requestUrl) {
	$len = strlen ( $baseUrl );
	if ($len == 0)
		return "";
	if(substr($baseUrl, -1)=='/') {
		return substr($baseUrl,0,$len-1).$requestUrl;
	}
	return $baseUrl.$requestUrl;
}

//echo str_replace('\\','/',realpath(dirname(__FILE__).'/'))."/upload/".time();
?>
<!DOCTYPE html>
<html lang="zh-CN" style="height: 100%">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>API测试接口</title>
<link rel="icon" type="image/x-icon" href="./MinPHP/res/favicon.ico">
<link href="./MinPHP/res/bootstrap-3.3.4-dist/css/bootstrap.min.css"
	rel="stylesheet">
<link href="./MinPHP/res/style.css" rel="stylesheet">
<!--[if lt IE 9]>
    <script src="./MinPHP/res/html5shiv.min.js"></script>
    <script src="./MinPHP/res/respond.min.js"></script>
    <![endif]-->
</head>

<body style="height: 100%">
	<div class="container-fluid" style="background: white; height: 90%;">
		<div class="row">
			<!--左侧导航end-->
			<div class="col-md-9"
				style="height: 100%; background: white; margin: 0px; overflow-y: auto; padding: 0px;">
				<!--顶部导航start-->
				<div class="textshadow"
					style="font-size: 16px; widht: 100%; height: 60px; line-height: 60px; padding: 0 16px 0 16px;; border-bottom: #ddd 1px solid">
					<span><?php echo $row['name']?></span>
				</div>
				<!--顶部导航end-->
				<!--主窗口start-->
				<div style="padding: 16px;">
					<form
						action="./req.php"
						method="post" class="form-horizontal" target="ifm_result" id="form1">
						<input type="hidden" name="testMethod" id="testMethod" value="0"/>
						<?php if(is_supper()){?>
						<div class="form-group">
							<label for="req_url" class="col-sm-2 control-label">本地地址</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="local_url"
									placeholder="本地地址" name="local_url"
									value="<?php echo getRealUrl($localUrl, $row['url'])?>">
							</div>
						</div>
						<?php }?>
						<div class="form-group">
							<label for="req_url" class="col-sm-2 control-label">请求地址</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="req_url"
									placeholder="请求地址" name="req_url"
									value="<?php echo getRealUrl($row['devUrl'], $row['url'])?>">
							</div>
						</div>
						
						<?php 
							for($i=0;$i<count($parameter['name']);$i++){
						?>
						<div class="form-group">
							<label for="<?php echo $parameter['name'][$i]?>" class="col-sm-2 control-label"><?php echo $parameter['prp'][$i]?></label>
							<div class="col-sm-10">
							<?php if($parameter['type'][$i]=='file'){?>
							<input type="file"<?php if($parameter['must'][$i]=='Y'){echo ' required="required"';}?> name="<?php echo $parameter['name'][$i]?>" class="form-control" id="<?php echo $parameter['name'][$i]?>"/>
							<?php }else {?>
							<input type="text"<?php if($parameter['must'][$i]=='Y'){echo ' required="required"';}?> name="<?php echo $parameter['name'][$i]?>" class="form-control" id="<?php echo $parameter['name'][$i]?>" placeholder="<?php echo $parameter['prp'][$i]?>" value="<?php echo $parameter['default'][$i]?>">	
							<?php }?>
							</div>
						</div>
						<?php }?>
						
						 <div class="form-group">
						    <div class="col-sm-offset-2 col-sm-10">
						      <button type="submit" class="btn btn-primary btn-lg" onclick="return localTest(0)">提交</button>
						      <?php if(is_supper()){?>
						      <button type="submit" class="btn btn-info btn-lg" onclick="return localTest(1)">本地测试</button>
						      <script type="text/javascript">
					function localTest(flag){
						if(flag==1 && $("#local_url").val().length==0){
							alert("本机测试地址未设置，请设置");
							return false;
						}
						$("#testMethod").val(flag);
						return true;
					}

						      </script>
						      <?php }?>
						    </div>
						 </div>
				</div>
				</form>
			</div>
			<!--主窗口end-->
			<iframe src=""  scroling="yes" width="100%" height="800px" name="ifm_result" id="ifm_result" frameborder="0"></iframe>
		</div>
	</div>
	</div>
	<script src="./MinPHP/res/jquery.min.js"></script>
	<script src="./MinPHP/res/bootstrap-3.3.4-dist/js/bootstrap.min.js"></script>
<script type="text/javascript">
$("document").ready(function(){
	if($("#form1").find(":file").length>0){
		$("#form1").attr("enctype","multipart/form-data");
	}
	
});

</script>
</body>
</html>
